<template>
    <div class="goods">
    <my-bread :breadList="breadList"></my-bread>
        <el-card>
      <el-row>
        <el-col :span="13">
          <el-input placeholder="请输入内容" v-model="getGoodsParams.query" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3" :offset="1">
          <el-button @click="$router.push('/goods/add')"> 添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column label="创建时间">
            <template slot-scope="scope">
                <p>{{scope.row.add_time | timer}}</p>
            </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              circle
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              circle
              @click="del(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getGoodsParams.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="getGoodsParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    </div>
</template>

<script>
import _ from "lodash"
import { getgoods,delgoods} from "@/http/goods.js";
export default {
  data(){
    return {
       breadList:[{
      "path":"/home",
      title:"首页"
      },{
      "path":"",
      title:"商品管理"
      },{
      "path":"/goods",
      title:"商品列表"
      }],
      total:0,
      goodsList:[],
      getGoodsParams: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
    }
  },
  created(){
     this.getgoods()
  },
  // 处理分页bug
  watch: {
    goodsList: {
      handler(n) {
        if (this.getGoodsParams.pagenum > 1 && n.length == 0) {
          this.getGoodsParams.pagenum -= 1;
          this.getgoods()
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods:{
    //删除商品
     async del(goods_id) {
      console.log(goods_id);
      let res = await delgoods(goods_id);
       this.getgoods()
    },
    //搜索框
     search: _.debounce(function() {
      console.log("hhh", this);
      this.getGoodsParams.pagenum = 1;
      this.getgoods()
    }, 2000),
    //获取商品信息
    async getgoods(){
      let res=await  getgoods(this.getGoodsParams)
      console.log(res);
      this.goodsList=res.data.goods
      this.total = res.data.total;
    },
    //分页
    handleSizeChange(val) {
      this.getGoodsParams.pagesize = val;
      this.getgoods()
    },
    handleCurrentChange(val) {
   this.getGoodsParams.pagenum = val;
      this.getgoods()
    },
  }
}
</script>

<style lang="scss">

</style>
